package configuration

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"os"
)

type Config struct {
	Profile string
	Port    int      `yaml:"port"`
	DB      DbConfig `yaml:"db"`
	LogDir  string   `yaml:"log_dir"`
}

type DbConfig struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	DbName   string `yaml:"db_name"`
}

var AppConfig *Config

func YamlConfig(profile string, basePath string) *Config {
	//读取yaml文件到缓存中
	yamlConfig, err := os.ReadFile(fmt.Sprintf(basePath+"application-%s.yaml", profile))
	if err != nil {
		panic(err)
	}

	c := &Config{Profile: profile}
	//yaml文件内容影射到结构体中
	err = yaml.Unmarshal(yamlConfig, c)
	if err != nil {
		panic(err)
	}

	AppConfig = c
	return c
}
